package com.cdd.common.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ReflectUtil;
import com.github.pagehelper.Page;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class BeanUtils extends BeanUtil {

	public static <T> List<T> copyPageToList(Collection<?> collection, Class<T> targetType) {
		if (collection instanceof Page) {
			Page<T> page = (Page<T>) collection;
			CopyOptions copyOptions = CopyOptions.create();
			List<T> list = collection.stream().map((source) -> {
				T target = ReflectUtil.newInstanceIfPossible(targetType);
				copyProperties(source, target, copyOptions);
				return target;
			}).collect(Collectors.toList());
			for (int i = 0; i < list.size(); i++) {
				page.set(i, list.get(i));
			}
			return page;
		}
		return copyToList(collection, targetType);
	}

}
